import {
  Entity,
  Column,
  OneToMany,
  Index,
  JoinColumn,
  ManyToOne,
  ManyToMany,
  JoinTable,
} from 'typeorm';
import { CommonEntity, CommonStatus } from '~/common/entity/common.entity';
import { ArticleCategoryEntity } from '~/modules/article/article-category/article-category.entity';
import { MissionEntity } from '~/modules/mission/mission.entity';
import { UserEntity } from '~/modules/user/user.entity';
import { ArticleContentEntity } from '../article/article-content/article-content.entity';
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';
import { UserAdminEntity } from '../user/user-admin/user-admin.entity';
import { BannerEntity } from '../banner/banner.entity';
import { SurveyEntity } from '../survey/survey.entity';
import { PointsGoodsEntity } from '../points/points-goods/points-goods.entity';
import { PointsEntity } from '../points/points.entity';

@Entity({ name: 'ah_tenant' })
@Index(['id'])
export class TenantEntity extends CommonEntity {
  // 头像
  @Column({ type: 'varchar', length: 255, comment: '头像', nullable: true })
  avatar: string;

  @ApiProperty({ description: '租户名称' })
  @IsString()
  @Column({ type: 'varchar', length: 50, comment: '租户名称' })
  name: string;

  @Column({ type: 'varchar', unique: true, length: 50, comment: '租户编码' })
  code: string;

  @Column({ type: 'varchar', length: 100, comment: '地址', nullable: true })
  address: string;

  @Column({ type: 'varchar', length: 50, comment: '联系人', nullable: true })
  contact: string;

  @Column({ type: 'varchar', length: 20, comment: '联系电话', nullable: true })
  phone: string;

  @Column({ type: 'varchar', length: 20, comment: '客服电话', name: 'service_phone', nullable: true })
  servicePhone: string;

  // 工作时间
  @Column({ type: 'varchar', length: 50, comment: '工作时间', name: 'work_time', nullable: true })
  workTime: string;

  @Column({ type: 'varchar', length: 50, comment: '邮箱', nullable: true })
  email: string;

  @Column({type: 'tinyint', width: 1, comment: '状态', default: CommonStatus.YES })
  status: CommonStatus;

  @Column({ type: 'text', comment: '备注', nullable: true })
  remark: string;

  @Column({ type: 'datetime', comment: '过期时间', nullable: true, name: 'expire_time' })
  expireTime: Date;

  // 关于我们
  @Column({ type: 'text', comment: '关于我们', nullable: true, name: 'about_us' })
  aboutUs: string;

  @OneToMany(() => UserEntity, (user) => user.tenant)
  users: UserEntity[];

  // 与任务表是一对多关系
  @OneToMany(() => MissionEntity, (mission) => mission.tenant)
  missions: MissionEntity[];

  @OneToMany(() => ArticleCategoryEntity, (articleCategory) => articleCategory.tenant)
  articleCategories: ArticleCategoryEntity[];

  @OneToMany(() => ArticleContentEntity, (articleContent) => articleContent.tenant)
  articleContents: ArticleContentEntity[];

  @OneToMany(() => BannerEntity, (banner) => banner.tenant)
  banners: BannerEntity[];

  @ManyToMany(() => UserAdminEntity, (admin) => admin.tenants)
  @JoinTable({
    name: 'ah_tenant_admin',
    joinColumn: { name: 'tenant_id' },
    inverseJoinColumn: { name: 'admin_id' },
  })
  admin: UserAdminEntity;

  @OneToMany(() => PointsGoodsEntity, (pointsGoods) => pointsGoods.tenant)
  goods: PointsGoodsEntity[];

  @OneToMany(() => PointsEntity, (points) => points.tenant)
  points: PointsEntity[];
}
